# Snippets for Puppet

global !p
import vim
import os.path
def get_module_namespace_and_basename():
    """This function will try to guess the current class or define name you are
    trying to create. Note that for this to work you should be using the module
    structure as per the style guide. Examples inputs and it's output
    * /home/nikolavp/puppet/modules/collectd/manifests/init.pp -> collectd
    * /home/nikolavp/puppet/modules/collectd/manfistes/mysql.pp -> collectd::mysql
    """
    current_file_path_without_ext = vim.eval('expand("%:p:r")') or ""
    if not current_file_path_without_ext:
        return "name"
    parts = os.path.split(current_file_path_without_ext)
    namespace = ''
    while parts[0] and parts[0] != '/':
        # Skip the init.pp files because they don't create a deeper namespace
        # and are special. Note this might break things like
        # modules/collectd/init/test.pp. But I am not even sure if puppet will
        # work for those.
        if parts[1] == 'init':
            parts = os.path.split(parts[0])
            continue
        if parts[1] == 'manifests':
            return os.path.split(parts[0])[1] + '::' + namespace.rstrip(':')
        else:
            namespace = parts[1] + '::' + namespace
        parts = os.path.split(parts[0])
    # couldn't guess the namespace. The user is editing a raw file in no module like the site.pp file
    return "name"
endglobal

snippet define "Definition" b
define ${1:`!p snip.rv = get_module_namespace_and_basename()`} {
  ${0:# body}
}

endsnippet

snippet class "Class declaration" b
class ${1:`!p snip.rv = get_module_namespace_and_basename()`} {
  ${0:# body}
}
endsnippet

snippet File "Defaults for file" b
File {
  owner => ${1:username},
  group => ${2:groupname},
}
endsnippet

# Resource types
snippet package "Package resource type" b
package { '${1:name}':
  ensure => ${2:installed},
}
endsnippet

snippet file "File resource type" b
file { '${1:name}':
  source => "puppet://${2:path}",
  mode   => ${3:mode},
endsnippet

snippet group "Group resource type" b
group { '${1:groupname}':
  ensure => ${3:present},
  gid    => ${2:gid},
endsnippet

snippet user "user resource type" b
user { '${1:username}':
  ensure     => ${2:present},
  uid        => ${3:uid},
  gid        => ${4:gid},
  comment    => ${5:gecos},
  home       => ${6:homedirectory},
  managehome => false,
  require    => Group['${7:group'}],
endsnippet

snippet exec "Exec resource type" b
exec { '${1:command}':
  refreshonly => true,
}
endsnippet

snippet cron "Cron resource type" b
cron { '${1:name}':
  user    => ${2:user},
  command => '${3:command}',
  minute  => ${3:minute},
  hour    => ${4:hour},
}
endsnippet

snippet mount "Mount resource type" b
mount { '${1:path}':
  device  => '${2:/dev}',
  fstype  => '${3:filesystem}',
  ensure  => mounted,
  options => 'rw,errors=remount-ro',
}
endsnippet

snippet service "Service resource type" b
service { '${1:name}':
  hasstatus => true,
  enable    => true,
  ensure    => running,
}
endsnippet

# vim:ft=snippets:
